package com.xueyi.operations.service.impl;

import com.xueyi.common.core.constant.basic.SqlConstants;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.web.correlate.domain.SqlField;
import com.xueyi.operations.domain.dto.OpFaultAcceptanceDto;
import com.xueyi.operations.domain.dto.OpFaultCloseDto;
import com.xueyi.operations.domain.dto.OpFaultProcessDto;
import com.xueyi.operations.domain.dto.OpFaultResultDto;
import com.xueyi.operations.domain.enums.FaultProcessEnum;
import com.xueyi.operations.domain.query.OpFaultProcessQuery;
import com.xueyi.operations.domain.correlate.OpFaultProcessCorrelate;
import com.xueyi.operations.manager.IOpFaultAcceptanceManager;
import com.xueyi.operations.manager.IOpFaultCloseManager;
import com.xueyi.operations.manager.IOpFaultResultManager;
import com.xueyi.operations.service.IOpFaultProcessService;
import com.xueyi.operations.manager.IOpFaultProcessManager;
import com.xueyi.common.web.entity.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
/**
 * 流程处理管理 服务层处理
 *
 * @author lwb
 */
@Service
public class OpFaultProcessServiceImpl extends BaseServiceImpl<OpFaultProcessQuery, OpFaultProcessDto, OpFaultProcessCorrelate, IOpFaultProcessManager> implements IOpFaultProcessService {

    @Autowired
    IOpFaultAcceptanceManager iOpFaultAcceptanceManager;

    @Autowired
    IOpFaultResultManager iOpFaultResultManager;

    @Autowired
    IOpFaultCloseManager iOpFaultCloseManager;

    /**
     * 查询故障处理对象列表 | 数据权限
     *
     * @param opFaultProcess 故障处理对象
     * @return 故障处理对象集合
     */
    @Override
    //@DataScope(userAlias = "createBy", mapperScope = {"OpFaultProcessMapper"})
    public List<OpFaultProcessDto> selectListScope(OpFaultProcessQuery opFaultProcess) {
        return super.selectListScope(opFaultProcess);
    }

    @Override
    public AjaxResult dispose(OpFaultAcceptanceDto opFaultAcceptanceDto) {

        OpFaultProcessDto opFaultProcessDto = super.baseManager.selectById(opFaultAcceptanceDto.getProcessId());
        if(opFaultProcessDto == null){
            AjaxResult.error("未找到流程工单。");
        }
        int row = iOpFaultAcceptanceManager.insert(opFaultAcceptanceDto);
        if(row != 1){
            AjaxResult.error("插入数据失败。");
            throw new RuntimeException("插入数据失败");
        }
        // 判断是否受理
        if(opFaultAcceptanceDto.getAcceptorType().equals("1")){
            opFaultProcessDto.setFaultProcessStatus(FaultProcessEnum.PROCESSING);
        }else{
            opFaultProcessDto.setFaultProcessStatus(FaultProcessEnum.FINISHED);
        }
        super.baseManager.update(opFaultProcessDto);
        return AjaxResult.success("受理完成。");
    }

    @Override
    public AjaxResult disposeResult(OpFaultResultDto opFaultResultDto) {
        OpFaultProcessDto opFaultProcessDto = super.baseManager.selectById(opFaultResultDto.getProcessId());
        if(opFaultProcessDto == null){
            AjaxResult.error("未找到流程工单。");
        }

        int row = iOpFaultResultManager.insert(opFaultResultDto);
        if(row != 1){
            AjaxResult.error("插入数据失败。");
            throw new RuntimeException("插入数据失败");
        }
        opFaultProcessDto.setFaultProcessStatus(FaultProcessEnum.UNCLOSED);
        super.baseManager.update(opFaultProcessDto);

        return AjaxResult.success("处理完成。");
    }

    @Override
    public AjaxResult disposeClose(OpFaultCloseDto opFaultCloseDto){

        OpFaultProcessDto opFaultProcessDto = super.baseManager.selectById(opFaultCloseDto.getProcessId());
        if(opFaultProcessDto == null){
            AjaxResult.error("未找到流程工单。");
        }
        int row = iOpFaultCloseManager.insert(opFaultCloseDto);
        if(row != 1){
            AjaxResult.error("插入数据失败。");
            throw new RuntimeException("插入数据失败");
        }
        if(opFaultCloseDto.getProcessingStatus().equals("1")){
            opFaultProcessDto.setFaultProcessStatus(FaultProcessEnum.FINISHED);
        }else{
            opFaultProcessDto.setFaultProcessStatus(FaultProcessEnum.PROCESSING);
        }
        super.baseManager.update(opFaultProcessDto);
        return AjaxResult.success("关闭完成");
    }
    @Override
    public AjaxResult getAllInfo(Serializable id) {
        OpFaultProcessDto opFaultProcessDto = super.baseManager.selectById(id);
        if (opFaultProcessDto == null) {
            return AjaxResult.error("未找到流程工单。");
        }

        // 动态装配语句
        SqlField sqlField1 = new SqlField(SqlConstants.OperateType.EQ, "process_id", id);
        List<OpFaultAcceptanceDto> acceptanceDtoList = iOpFaultAcceptanceManager.selectListByField(sqlField1);

        opFaultProcessDto.setOpFaultAcceptanceDto(acceptanceDtoList.size()>0?acceptanceDtoList.get(0):null);

        SqlField sqlField2 = new SqlField(SqlConstants.OperateType.EQ, "process_id", id);
        List<OpFaultResultDto> resultDtoList = iOpFaultResultManager.selectListByField(sqlField2);
        opFaultProcessDto.setOpFaultResultDto(resultDtoList.size()>0?resultDtoList.get(0):null);

        SqlField sqlField3 = new SqlField(SqlConstants.OperateType.EQ, "process_id", id);
        List<OpFaultCloseDto> closeDtoList = iOpFaultCloseManager.selectListByField(sqlField3);
        opFaultProcessDto.setOpFaultCloseDto(closeDtoList.size()>0?closeDtoList.get(0):null);
        return AjaxResult.success(opFaultProcessDto);
    }
}
